#include "wifiset.h"
#define DEBUG_ON 0

IPAddress apIP(192, 168, 1, 1);
DNSServer dnsServer;
ESP8266WebServer webServer(80);

char loop_flag = 0;

void(* resetFunc) (void) = 0;

extern "C" bool wifi_init(void)
{
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  WiFi.softAP(AP_NAME);

  // 设置DNS服务的TTL数值为300
  dnsServer.setTTL(300);
  // 启动特定域名的DNS服务器
  dnsServer.start(DNS_PORT, "www.8266.com", apIP);
  
  //设置服务器页面信息
  webServer.on("/",web_page);
  webServer.onNotFound(callback_page);

  webServer.begin(); //启动web服务器

  loop_flag = 0;

  Serial.print("\n\n\nchar web_server(void):  ip->");
  Serial.println(WiFi.softAPIP());
  
  while(1)
  {
    dnsServer.processNextRequest();//处理DNS请求服务
    webServer.handleClient();
    if(loop_flag) break;
  }
  webServer.close();
  dnsServer.stop();  
  WiFi.mode(WIFI_STA); 
  save_file();
  #if DEBUG_ON
  Serial.print("char web_server(void) end!!! ");
  Serial.println(" ESP.restart(); ");
  read_file(); 
  #endif
  resetFunc();
}


extern "C" bool wifi_connect(void)
{
  int i = 0;
  read_file();
  WiFi.mode(WIFI_STA); 
  WiFi.begin(config_info.wifi_name,config_info.wifi_pass);

  delay(5000);
  if(WiFi.status()==WL_CONNECTED) {return 1;}

  while(WiFi.status()!=WL_CONNECTED)
  {
    WiFi.begin(config_info.wifi_name,config_info.wifi_pass);
    delay(5000);
  }
  
  return 0;
}
/***************************************************************/
static void web_page(void)
{
  #if DEBUG_ON
  Serial.println("main page !!!!");
  #endif
  Serial.println("main page !!!!");
  if(SPIFFS.exists(html_file_name))
  {
    File file = SPIFFS.open(html_file_name, "r"); 
    webServer.streamFile(file, "text/html");
    file.close();
  }
  else
  {
    webServer.send(404,"text/plain","webFile don't exist !!!");
  }      
}


static void callback_page(void)
{
 if(webServer.args() == 11)
 {
  webServer.send(200, "text/plane","success!!!");

  Serial.println("RESEIVE DATA !!!");
  loop_flag = 1;
  
  String temp;
  
  temp  = webServer.arg(0);
  memcpy(config_info.wifi_name,temp.c_str(),temp.length());
  
  temp  = webServer.arg(1);
  memcpy(config_info.wifi_pass,temp.c_str(),temp.length());
  
  temp  = webServer.arg(2);
  memcpy(config_info.bps,temp.c_str(),temp.length());

  temp  = webServer.arg(3);
  memcpy(config_info.server_ip,temp.c_str(),temp.length());

  temp  = webServer.arg(4);
  memcpy(config_info.server_port,temp.c_str(),temp.length());

  temp  = webServer.arg(5);
  memcpy(config_info.user_name,temp.c_str(),temp.length());

  temp  = webServer.arg(6);
  memcpy(config_info.user_psss,temp.c_str(),temp.length());

  temp  = webServer.arg(7);
  memcpy(config_info.device_id,temp.c_str(),temp.length());

  temp  = webServer.arg(8);
  memcpy(config_info.sub_topic,temp.c_str(),temp.length());

  temp  = webServer.arg(9);
  memcpy(config_info.pub_topic,temp.c_str(),temp.length());
 }
 #if DEBUG_ON
 Serial.println("callback page !!!!");
 Serial.println("*************************\nvoid callBack_page(void)");
 Serial.println("save file :\n*************************");
 
 Serial.println(webServer.argName(0));
 Serial.println(webServer.arg(0));
 Serial.println(webServer.argName(1));
 Serial.println(webServer.arg(1));
 Serial.println(webServer.argName(2));
 Serial.println(webServer.arg(2));
 Serial.println(webServer.argName(3));
 Serial.println(webServer.arg(3));
 Serial.println(webServer.argName(4));
 Serial.println(webServer.arg(4));
 Serial.println(webServer.argName(5));
 Serial.println(webServer.arg(5));
 Serial.println(webServer.argName(6));
 Serial.println(webServer.arg(6));
  Serial.println(webServer.argName(7));
 Serial.println(webServer.arg(7));
  Serial.println(webServer.argName(8));
 Serial.println(webServer.arg(8));
  Serial.println(webServer.argName(9));
 Serial.println(webServer.arg(9));
  Serial.println(webServer.argName(10));
 Serial.println(webServer.arg(10));
  Serial.println(webServer.argName(11));
 Serial.println(webServer.arg(11));
  
 #endif    
}
